<?php

namespace App\Traits;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Symfony\Component\HttpFoundation\File\UploadedFile;

trait UploadQiuniuTrait
{
    protected $token;

    public function __construct()
    {
        $accessKey = 'dE4EjR1lCHtjGb6NWf2NdBd09G2lQzURgplpZvvg';
        $secretKey = 'oztiALgBkTugMbiI1fAcTDfJws8wqs2SW58gv1aP';
        $this->host = 'of56r3q9n.bkt.clouddn.com';
        $auth = new Auth($accessKey, $secretKey);
        $bucket = 'colinwait';
        $this->token = $auth->uploadToken($bucket);



    }

    public function upload(UploadedFile $file)
    {
        $uploadMgr = new UploadManager();
        list($result, $error) = $uploadMgr->putFile($this->token, str_random(16), $file->getRealPath());

        if (isset($result['key'])) {
            return $result['key'];
        }

        return '/assets/system/images/default-album-cover.jpg';
    }
}